
var mongoose = require('mongoose')

var articleSchema = require('../model/index')

mongoose.connect('mongodb://localhost/node-douban-rent')

const db = mongoose.connection

const Models = {
  Article: mongoose.model('Article', articleSchema)
}

var now = new Date()
now.setDate(now.getDate() - 30)
var time2 = now.getTime()

db.on('error', function () {
  console.log('数据库链接出错')
})

db.once('open', function () {
  console.log('数据库链接完成')
})

Models.Article.find(function (err, articles) {
  if (err) {
    console.log('find' + err)
  }
  let artArr = []
  articles.forEach(ele => {
    if (artArr.indexOf(ele.topicUrl) === -1) {
      artArr.push(ele.topicUrl)
    } else {
      Models.Article.deleteOne({topicUrl: ele.topicUrl}, function (err) {
        if (err) {
          console.log('deleteOne' + err)
        }
      })
    }
  })
  console.log('去除重复数据完成')
})

Models.Article.remove({$or: [{publicData: {'$lt': time2}}, {title: ''}]}, function (err) {
  if (err) {
    console.log('remove' + err)
  }

  console.log('去除过期数据完成和title为空的数据完成')
})

module.exports = Models
